<?php

include_once APPLICATION_DIR . "/application/model/VIDEO_PROVIDER.php";

class Video_Provider extends DataObjects_VIDEO_PROVIDER
{
	function getList($filter = array(), &$pager=false)
	{
		$video_provider = new Video_Provider();
		if (isset($filter['name']) && $filter['name']) {
			$video_provider->whereAdd("NAME LIKE '%".mysql_real_escape_string($filter['name'])."%'");
		}
        if (isset($filter['id_country']) && $filter['id_country']) {
            $video_provider->selectAdd();
            $video_provider->selectAdd("VIDEO_PROVIDER.*");
            $video_provider->joinAdd(new Provider_Country(),'INNER',false,false,"VIDEO_PROVIDER.IS_DELETED = ".IS_NOT_DELETED);
            $video_provider->whereAdd("PROVIDER_COUNTRY.ID_COUNTRY  = ".$filter['id_country']);
		}
		$video_provider->whereAdd("IS_DELETED = ".IS_NOT_DELETED);
		$video_provider->orderBy("NAME");
		if ($pager == true) {
			$pager = new DBPager($video_provider, $filter, true);
		}
		
		$video_provider->find();

		return $video_provider;
	}

	public function isExistsByName($name= '', $id = 0)
	{
		if ($name == '')
		{
			return false;
		}	
		$video_provider = new Video_Provider();
		if ($id) {
			$video_provider->whereAdd("ID <> {$id}");
		}
		$video_provider->NAME = $name;
		$video_provider->find();
		return $video_provider->N > 0;
	}

	public function validateInput(){
		global $error_msg;

		if (!textvalid($this->NAME))
		{
			$error_msg->add(_MSG_NAME_NOT_EMPTY);
		}

		if(!Utils::isValidLength($this->NAME, MAX_LENGTH_NAME))
		{
			$error_msg->add(_LB_INVALID_LENGTH_NAME);		
		}
		
		if (self::isExistsByName($this->NAME, $this->ID))
		{
			$error_msg->add(_MSG_NAME_ALREADY_EXISTS);
		}

		if ($this->PROVIDER_STATUS != VIDEO_PROVIDER_STATUS_CREATED && $this->PROVIDER_STATUS != VIDEO_PROVIDER_STATUS_CONNECTED) {
			$error_msg->add(_MSG_INVALID_STATUS);
		}
//        if (!Request::getIntNumber("default_position")) {
//            if (!textvalid($this->WIDTH)) {
//                $error_msg->add(_MSG_THUMB_WIDTH_NOT_EMPTY);
//            }
//            if ($this->WIDTH <= 0 || !is_integer($this->WIDTH)) {
//                $error_msg->add(_MSG_THUMB_WIDTH_INVALID_NUMBER);
//            }
//            if (!textvalid($this->HEIGHT)) {
//                $error_msg->add(_MSG_THUMB_HEIGHT_NOT_EMPTY);
//            }
//            if ($this->HEIGHT <= 0 || !is_integer($this->HEIGHT)) {
//                $error_msg->add(_MSG_THUMB_HEIGHT_INVALID_NUMBER);
//            }
//            if ($this->POS_X < 0 || !is_numeric($this->POS_X)) {
//                $error_msg->add(_MSG_POS_X_INVALID_NUMBER);
//            }
//            if ($this->POS_Y < 0 || !is_numeric($this->POS_Y)) {
//                $error_msg->add(_MSG_POS_Y_INVALID_NUMBER);
//            }
//        }
        
        if (!textvalid($this->REF_TYPE))
		{
			$error_msg->add(_MSG_REF_TYPE_NOT_VALID);
		}
        else if($this->REF_TYPE != URL_TEMPLATE_TYPE_DONT_CHANGE
            && $this->REF_TYPE != URL_TEMPLATE_TYPE_REPLACE_BY_MO
            && $this->REF_TYPE != URL_TEMPLATE_TYPE_REPLACE_PARAM_BY_MO
            && $this->REF_TYPE != URL_TEMPLATE_TYPE_ADD_MO_END
            && $this->REF_TYPE != URL_TEMPLATE_TYPE_ADD_MO_AS_PARAM_END)
        {
            $error_msg->add(_MSG_REF_TYPE_NOT_VALID);
        }
        if($this->REF_TYPE == URL_TEMPLATE_TYPE_REPLACE_BY_MO
            || $this->REF_TYPE == URL_TEMPLATE_TYPE_REPLACE_PARAM_BY_MO
            || $this->REF_TYPE == URL_TEMPLATE_TYPE_ADD_MO_AS_PARAM_END)
        {
            if (!textvalid($this->REF_TEXT)||!preg_match('/^[0-9a-zA-Z_]+$/', $this->REF_TEXT))
            {
                $error_msg->add(_MSG_REF_TEXT_NOT_VALID);
            }
        }
        if($this->DEFAULT_BEHAVIOR != "" && $this->DEFAULT_BEHAVIOR != REF_DEFAULT && $this->DEFAULT_BEHAVIOR != REF_DEFAULT_WITH_PREFIX && $this->DEFAULT_BEHAVIOR != REF_MO_REFERENCE){
            $error_msg->add(_MSG_INVALID_LB_DEFAULT_BEHAVIOR);
        }
        if ($this->DEFAULT_BEHAVIOR == REF_DEFAULT_WITH_PREFIX) {
           if (!textvalid($this->REF_PREFIX)||!preg_match('/^[0-9a-zA-Z_]+$/', $this->REF_PREFIX))
            {
                $error_msg->add(_MSG_REF_PREFIX_NOT_VALID);
            }
        }
        
		return $error_msg->isNoError();
	}

	function delete()
	{
		if (!$this->IS_DELETED && self::isDeleteAble($this->ID)) {
			$this->NAME = $this->ID.'_'.$this->NAME;
			$this->IS_DELETED = IS_DELETED;
			$this->STATUS = STATUS_TO_SYNCHRONIZE;
			$this->update();
		}
	}
	
	public function isDeleteAble($id = 0) {
		$deletable = true;

		$provider_category = new Provider_Category();
		$provider_category->ID_PROVIDER = $id;
		$provider_category->find();

		if ($provider_category->N > 0) {
			$deletable = false;
		}
		
		$connector = new Connector();
		$connector->ID_PROVIDER = $id;
		$connector->find();

		if ($connector->N > 0) {
			$deletable = false;
		}
		
		$provider_country = new Provider_Country();
		$provider_country->ID_PROVIDER = $id;
		$provider_country->find();
		
		if ($provider_country->N > 0) {
			$deletable = false;
		}
		
        $provider_mo_reference = new Provider_Mo_Reference();
		$provider_mo_reference->ID_PROVIDER = $id;
		$provider_mo_reference->IS_DELETED = IS_NOT_DELETED;
		$provider_mo_reference->find();
		
		if ($provider_mo_reference->N > 0) {
			$deletable = false;
		}
        
		return $deletable;
	}

	/**
	* Get extension of an image file, based on the mime type
	*
	* @param string $filename
	* @return string
	*/
	function getImageType($filename)
	{
		$image_info = getimagesize($filename);
		$type = $image_info['mime'];

		switch($type)
		{
		   case 'image/pjpeg':
		   case 'image/jpeg':
		   case 'image/jpg':
		 		return '.jpg';
		 		break;
		   case 'image/png':
		   case 'image/x-png':
		   		return '.png';
		   		break;
		   case 'image/gif':
		   		return '.gif';
		   		break;
		   case 'image/bmp':
		   		return '.bmp';
		   		break;
		   default:
		   		return '';
		   		break;
		}
	}
    
    function isValidURL($url) {
        return preg_match("/^({PURCHASE_URL}){1}([\/]?)(((([\?\&]{1})[a-zA-Z0-9_]+)(=))?)(({ref\|\'([a-zA-Z0-9_\s]+)\'})?)$/", $url);
    }
    
    function getProviderByName($name = '') {
        if (!$name) return null;
        
        $provider = new Video_Provider();
        $provider->NAME = $name;
        $provider->find(1);
        return $provider;
    }
}
